<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户端ID" prop="clientId">
        <el-input
          v-model="queryParams.clientId"
          placeholder="请输入客户端ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拥有资源" prop="resourceIds">
        <el-input
          v-model="queryParams.resourceIds"
          placeholder="请输入拥有资源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请权限" prop="scope">
        <el-input
          v-model="queryParams.scope"
          placeholder="请输入申请权限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跳过授权" prop="autoapprove">
        <el-select v-model="queryParams.autoapprove" placeholder="请选择跳过授权" clearable size="small">
          <el-option
            v-for="dict in autoapproveOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['usercenter:clentDetails:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['usercenter:clentDetails:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['usercenter:clentDetails:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['usercenter:clentDetails:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clentDetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户端ID" align="center" prop="clientId" />
      <el-table-column label="拥有资源" align="center" prop="resourceIds" />
      <el-table-column label="申请权限" align="center" prop="scope" />
      <el-table-column label="授权类型" align="center" prop="authorizedGrantTypes" :formatter="authorizedGrantTypesFormat" />
      <el-table-column label="重定向URI" align="center" prop="webServerRedirectUri" />
      <el-table-column label="拥有权限" align="center" prop="authorities" />
      <el-table-column label="令牌有效时间" align="center" prop="accessTokenValidity" />
      <el-table-column label="刷新令牌有效时间" align="center" prop="refreshTokenValidity" />
      <el-table-column label="跳过授权" align="center" prop="autoapprove" :formatter="autoapproveFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="resetSecret(scope.row)"
            v-hasPermi="['usercenter:clentDetails:edit']"
          >重置密钥</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['usercenter:clentDetails:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['usercenter:clentDetails:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改oauth2客户端信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户端ID" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户端ID" />
        </el-form-item>
        <el-form-item label="拥有资源" prop="resourceIds">
          <el-input v-model="form.resourceIds" placeholder="请输入拥有资源" />
        </el-form-item>
        <el-form-item label="申请权限" prop="scope">
          <el-input v-model="form.scope" placeholder="请输入申请权限" />
        </el-form-item>
        <el-form-item label="授权类型">
          <el-checkbox-group v-model="form.authorizedGrantTypes">
            <el-checkbox
              v-for="dict in authorizedGrantTypesOptions"
              :key="dict.dictValue"
              :label="dict.dictValue">
              {{dict.dictLabel}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="重定向URI" prop="webServerRedirectUri">
          <el-input v-model="form.webServerRedirectUri" placeholder="请输入重定向URI" />
        </el-form-item>
        <el-form-item label="拥有权限" prop="authorities">
          <el-input v-model="form.authorities" placeholder="请输入拥有权限" />
        </el-form-item>
        <el-form-item label="跳过授权" prop="autoapprove">
          <el-select v-model="form.autoapprove" placeholder="请选择跳过授权">
            <el-option
              v-for="dict in autoapproveOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 重置密钥对话框 -->
    <el-dialog :title="title" :visible.sync="resetSecretOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-input v-model="resetSecretParam.clientSecret" placeholder="请输入新的客户端密钥" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resetSecretSubmitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listClentDetails, getClentDetails, delClentDetails, addClentDetails, updateClentDetails,  resetSecret } from "@/api/usercenter/clentDetails";

export default {
  name: "ClentDetails",
  components: {
  },
  data() {
    return {
      resetSecretParam: {
          //密钥
          clientSecret: "",
          //客户端id
          clientId: ""
      },
      //是否显示重置密钥弹出层
      resetSecretOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // oauth2客户端信息表格数据
      clentDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 授权类型字典
      authorizedGrantTypesOptions: [],
      // 跳过授权字典
      autoapproveOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientId: null,
        resourceIds: null,
        scope: null,
        authorizedGrantTypes: null,
        autoapprove: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientId: [
          { required: true, message: "客户端ID不能为空", trigger: "blur" }
        ],
        authorizedGrantTypes: [
          { required: true, message: "授权类型不能为空", trigger: "blur" }
        ],
        autoapprove: [
          { required: true, message: "跳过授权不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_grant_type").then(response => {
      this.authorizedGrantTypesOptions = response.data;
    });
    this.getDicts("oauth_autoapprove").then(response => {
      this.autoapproveOptions = response.data;
    });
  },
  methods: {
    /** 查询oauth2客户端信息列表 */
    getList() {
      this.loading = true;
      listClentDetails(this.queryParams).then(response => {
        this.clentDetailsList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 授权类型字典翻译
    authorizedGrantTypesFormat(row, column) {
      return this.selectDictLabels(this.authorizedGrantTypesOptions, row.authorizedGrantTypes);
    },
    // 跳过授权字典翻译
    autoapproveFormat(row, column) {
      return this.selectDictLabel(this.autoapproveOptions, row.autoapprove);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.resetSecretOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientId: null,
        resourceIds: null,
        clientSecret: null,
        scope: null,
        authorizedGrantTypes: [],
        webServerRedirectUri: null,
        authorities: null,
        accessTokenValidity: null,
        refreshTokenValidity: null,
        additionalInformation: null,
        autoapprove: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加oauth2客户端信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clientId = row.clientId || this.ids
      getClentDetails(clientId).then(response => {
        this.form = response.data;
        this.form.authorizedGrantTypes = this.form.authorizedGrantTypes.split(",");
        this.open = true;
        this.title = "修改oauth2客户端信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.authorizedGrantTypes = this.form.authorizedGrantTypes.join(",");
          if (this.form.clientSecret != null) {
            updateClentDetails(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClentDetails(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 重置密钥按钮操作 */
    resetSecret(row) {
      this.reset();
      this.resetSecretParam.clientId = row.clientId || this.ids
      this.resetSecretOpen = true;
      this.title = "重置客户端密钥";
    },
    /** 重置密钥提交按钮 */
    resetSecretSubmitForm() {
      resetSecret(this.resetSecretParam).then(response => {
      this.msgSuccess("重置成功");
                    this.open = false;
                    this.getList();
      });

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clientIds = row.clientId || this.ids;
      this.$confirm('是否确认删除oauth2客户端信息编号为"' + clientIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClentDetails(clientIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('usercenter/clentDetails/export', {
        ...this.queryParams
      }, `usercenter_clentDetails.xlsx`)
    }
  }
};
</script>
